package www.dashu.com.controller;

import www.dashu.com.api.CommonResult;
import www.dashu.com.service.RedisService;
import www.dashu.com.domain.CProductCategory;
import www.dashu.com.dto.CProductCategoryQuery;
import www.dashu.com.service.CProductCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.lang.reflect.Field;
import java.util.*;

@Controller
@Api(tags = "CProductCategoryController", description = "商品分类")
@RequestMapping("/productCategory")
public class CProductCategoryController
{

    @Autowired
    CProductCategoryService productCategoryService;
    @Autowired
    RedisService redisService;

    @ApiOperation(value = "商品分类列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<Page<CProductCategory>> list(CProductCategoryQuery query) {

        try {
            Page<CProductCategory> pageData = productCategoryService.findAll(query);
            return CommonResult.success(pageData);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    @ApiOperation(value = "新增商品分类")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult create(@RequestBody CProductCategory productCategory) {
        productCategory.setId(productCategoryService.createId(productCategory));

        Field[] fields = productCategory.getClass().getDeclaredFields();
        for(Field field : fields){
            field.setAccessible(true);
            if(field.getName().toLowerCase().contains("time")){
                try {
                    field.set(productCategory, new Date());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        productCategoryService.save(productCategory);
        return CommonResult.success();
    }

    @ApiOperation(value = "修改商品分类")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult update(@RequestBody CProductCategory productCategory) {

        Field[] fields = productCategory.getClass().getDeclaredFields();
        for(Field field : fields){
            field.setAccessible(true);
            if(field.getName().toLowerCase().contains("updatetime")){
                try {
                    field.set(productCategory, new Date());
                 } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        productCategoryService.save(productCategory);
        return CommonResult.success();
    }

    @ApiOperation(value = "删除商品分类")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    @Transactional(propagation= Propagation.REQUIRED, rollbackFor = Exception.class)
    public CommonResult delete(@RequestBody CProductCategory productCategory) {
        productCategoryService.deleteById(productCategory.getId());
        return CommonResult.success();
    }

    @ApiOperation(value = "分类树形数据")
    @RequestMapping(value = "/tree", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<List<CProductCategory>> tree(@RequestBody Map<String, String> params) {
        List<CProductCategory> productCategoryList = productCategoryService.findAllSort();

        List<CProductCategory> treeRoot = new ArrayList<>();
        Map<Long,CProductCategory> dicMap = new HashMap<>();
        productCategoryList.forEach(item -> {
            dicMap.put(item.getId(), item);
        });

        productCategoryList.forEach(item -> {
            Long pid = item.getParentId();
            CProductCategory parent = dicMap.get(pid);
            if(null == parent){
                treeRoot.add(item);
            }else {
                List<CProductCategory> children = parent.getChildren();
                if(null == children){
                    children = new ArrayList<>();
                }
                children.add(item);
                parent.setChildren(children);
            }
        });
        return CommonResult.success(treeRoot);
    }

}
